﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using FreedomERP.Models;
using FreedomERP.Comm;
using FreedomERP.Services.Bd;
using FreedomERP.Services.Bd.Impl;
using System.Collections;
using Ext.Net.MVC;
using FreedomERP.Comm.Utils;
using FreedomERP.Models.Bd;
using Ext.Net;
using FreedomERP.Filter;

namespace FreedomERP.Controllers
{
    [CheckSessionFilterAttribute]
    public class CustomerController : Controller
    {

        IBdCustomerService customerService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (customerService == null) { customerService = new BdCustomerServiceImpl(); }

            base.Initialize(requestContext);
        }

        //
        // GET: /Customer/

        public ActionResult CustomerIndex()
        {
            return View();
        }

        public AjaxStoreResult CustomerListPaging(String fId, String fNumber, String fName, String fContactor, String fContactPhone, String fContactMobile, String fContactEmail, String fContactAddress, int start, int limit)
        {
            int totalCount = 0;
            List<BdCustomerView> customers = null;
            try
            {
                string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();

                CustomerParameters customerParameters = new CustomerParameters();
                if (fId != null && !ConstData.EMPTY_STRING.Equals(fId))
                {
                    customerParameters.FId = fId;
                }
                if (fNumber != null && !ConstData.EMPTY_STRING.Equals(fNumber))
                {
                    customerParameters.FNumber = fNumber;
                }
                if (fName != null && !ConstData.EMPTY_STRING.Equals(fName))
                {
                    customerParameters.FName = fName;
                }
                if (fContactor != null && !ConstData.EMPTY_STRING.Equals(fContactor))
                {
                    customerParameters.FContactor = fContactor;
                }
                if (fContactPhone != null && !ConstData.EMPTY_STRING.Equals(fContactPhone))
                {
                    customerParameters.FContactPhone = fContactPhone;
                }
                if (fContactMobile != null && !ConstData.EMPTY_STRING.Equals(fContactMobile))
                {
                    customerParameters.FContactMobile = fContactMobile;
                }
                if (fContactEmail != null && !ConstData.EMPTY_STRING.Equals(fContactEmail))
                {
                    customerParameters.FContactEmail = fContactEmail;
                }
                if (fContactAddress != null && !ConstData.EMPTY_STRING.Equals(fContactAddress))
                {
                    customerParameters.FContactAddress = fContactAddress;
                }
                customerParameters.FOrgunitId = currAccountOrgunitFId;
                customerParameters.start = start;
                customerParameters.limit = limit;
                customers = customerService.findCustomerList(customerParameters, out totalCount);
            }
            catch (System.Exception ex)
            {
                customers = new List<BdCustomerView>();
            }
            AjaxStoreResult customersPaging = new AjaxStoreResult(customers, totalCount);
            return customersPaging;
        }

        public AjaxStoreResult findCustomerById(String fId)
        {
            int totalCount = 0;
            CustomerParameters customerParameters = new CustomerParameters();
            customerParameters.FId = fId;
            customerParameters.start = 0;
            customerParameters.limit = 1;
            customerParameters.FOrgunitId = BaseController.GetCurrLoginAccountOrgunitFId();
            List<BdCustomerView> customers = customerService.findCustomerList(customerParameters, out totalCount);
            AjaxStoreResult customersPaging = new AjaxStoreResult(customers, totalCount);
            return customersPaging;
        }


        public AjaxFormResult SaveOrUpadteCustomer(String fId, String fName, String fContactor, String fContactPhone, String fContactMobile, String fContactEmail, String fContactAddress)
        {
            String resultFID = null;
            AjaxFormResult response = new AjaxFormResult();
            string ResultMsg = string.Empty;
            bool ResultBool = true;
            try
            {
                string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();

                BdCustomerView customerView = new BdCustomerView();
                if (fId == null || fId.Equals(ConstData.EMPTY_STRING))
                {
                    customerView.FCreatorId = BaseController.GetCurrLoginAccountFId();
                    customerView.FModifierId = BaseController.GetCurrLoginAccountFId();
                    customerView.FCreateTime = DateTime.Now;
                    customerView.FModifyTime = customerView.FCreateTime;
                    customerView.FNumber = "CID" + DateUtils.GetCurDateFormat("yyyyMMddHHmmss");
                }
                else
                {
                    customerView.FId = fId;
                    customerView.FModifierId = BaseController.GetCurrLoginAccountFId();
                    customerView.FModifyTime = DateTime.Now;
                }
                //String orgunitId = null;
                IBdOrgunitService orgunitService = new BdOrgunitServiceImpl();
                //orgunitId = orgunitService.findOrgunitId(ConstData.ORGUNIT_NAME_LC);
                customerView.FOrgunitId = currAccountOrgunitFId;
                customerView.FName = fName;
                
                customerView.FContactAddress = fContactAddress;
                customerView.FContactEmail = fContactEmail;
                customerView.FContactor = fContactor;
                customerView.FContactPhone = fContactPhone.Replace("，",",");
                customerView.FContactMobile = fContactMobile.Replace("，", ","); ;
                customerService.saveOrUpdateCustomer(customerView, out resultFID);

            }
            catch (System.Exception ex)
            {
                response.Success = false;
                response.ExtraParams["msg"] = ex.Message;
                return response;
            }
            response.Success = ResultBool;
            response.ExtraParams["msg"] = resultFID;
            return response;
        }


        public ActionResult DeleteCustomer(String fId)
        {
            AjaxResult response = new AjaxResult();
            string ResultMsg = string.Empty;
            try
            {
                customerService.deleteCustomer(fId);
            }
            catch (System.Exception ex)
            {

                response.ErrorMessage = ex.Message;

            }
            return response;
        }

    }
}
